package com.md.config.database.slave.balancer;

import com.md.config.database.slave.DataSourceProperty;
import com.md.util.ObjectUtil;

import java.util.List;
import java.util.Random;

public class RandomSlaveSourceLoadBalancer extends ISlaveSourceLoadBalancer {
  private final Random random;

  public RandomSlaveSourceLoadBalancer() {
    random = new Random();
  }

  @Override
  public String load(final List<DataSourceProperty> dataSourceConfigs) {
    if (ObjectUtil.isEmpty(dataSourceConfigs)) {
      return null;
    }
    // 随机选取一个从库
    int index = random.nextInt(dataSourceConfigs.size());
    return dataSourceConfigs.get(index).getUnique();
  }
}
